home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Apps / ArchiveUtils / nx_arc / arcrun.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-12-20  |  3.2 KB  |  120 lines

  1. /*
  2.  * $Log:    arcrun.c,v $
  3.  * Revision 1.3  87/08/13  17:05:51  hyc
  4.  * Run thru indent, fixed some signed vs. unsigned problems
  5.  * with bp <-> buf, and inbuf and localbuf...
  6.  *  Revision 1.2  87/07/21  09:08:37  hyc *** empty
  7.  * log message ***
  8.  * 
  9.  */
  10.  
  11. /*
  12.  * ARC - Archive utility - ARCRUN
  13.  * 
  14.  * Version 1.20, created on 03/24/86 at 19:34:31
  15.  * 
  16.  * (C) COPYRIGHT 1985,85 by System Enhancement Associates; ALL RIGHTS RESERVED
  17.  * 
  18.  * By:  Thom Henderson
  19.  * 
  20.  * Description: This file contains the routines used to "run" a file which is
  21.  * stored in an archive.  At present, all we really do is (a) extract a
  22.  * temporary file, (b) give its name as a system command, and then (c) delete
  23.  * the file.
  24.  * 
  25.  * Language: Computer Innovations Optimizing C86
  26.  */
  27. #include <stdio.h>
  28. #include "arc.h"
  29.  
  30. INT
  31. runarc(num, arg)        /* run file from archive */
  32.     INT             num;    /* number of arguments */
  33.     char           *arg[];    /* pointers to arguments */
  34. {
  35.     struct heads    hdr;    /* file header */
  36.     char           *makefnam();    /* filename fixer */
  37.     char            buf[100];    /* filename buffer */
  38.     FILE           *fopen();/* file opener */
  39.     INT             runfile();
  40.  
  41.     rempath(num, arg);    /* strip off paths */
  42.  
  43.     openarc(0);        /* open archive for reading */
  44.  
  45.     if (num) {        /* if files were named */
  46.         while (readhdr(&hdr, arc)) {    /* while more files to check */
  47.             if (match(hdr.name, makefnam(arg[0], ".*", buf)))
  48.                 runfile(&hdr, num, &arg[1]);
  49.             else
  50.                 fseek(arc, hdr.size, 1);
  51.         }
  52.     } else
  53.         while (readhdr(&hdr, arc))    /* else run all files */
  54.             runfile(&hdr, 0, NULL);
  55.  
  56.     closearc(0);        /* close archive after changes */
  57. }
  58.  
  59. static          INT
  60. runfile(hdr, num, arg)        /* run a file */
  61.     struct heads   *hdr;    /* pointer to header data */
  62.     INT             num;    /* number of arguments */
  63.     char           *arg[];    /* pointers to arguments */
  64. {
  65.     FILE           *tmp, *fopen();    /* temporary file */
  66.     char            buf[100], *makefnam();    /* temp file name, fixer */
  67.     char            sys[100];    /* invocation command buffer */
  68.     char           *dir, *gcdir();    /* directory stuff */
  69.     INT             n;    /* index */
  70.  
  71.     /* makefnam("$ARCTEMP",hdr->name,buf); */
  72.     sprintf(buf, "%s.RUN", arctemp);
  73.  
  74. #ifdef MSDOS
  75.     if (!strcmp(buf, "$ARCTEMP.BAS"))
  76.         strcpy(sys, "BASICA $ARCTEMP");
  77.  
  78.     else if (!strcmp(buf, "$ARCTEMP.BAT")
  79.          || !strcmp(buf, "$ARCTEMP.COM")
  80.          || !strcmp(buf, "$ARCTEMP.EXE"))
  81.         strcpy(sys, "$ARCTEMP");
  82.  
  83.     else {
  84.         if (warn) {
  85.             printf("File %s is not a .BAS, .BAT, .COM, or .EXE\n",
  86.                    hdr->name);
  87.             nerrs++;
  88.         }
  89.         fseek(arc, hdr->size, 1);    /* skip this file */
  90.         return;
  91.     }
  92. #endif
  93.  
  94.     if (warn)
  95.         if (tmp = fopen(buf, "r"))
  96.             arc_abort("Temporary file %s already exists", buf);
  97.     if (!(tmp = fopen(buf, "w")))
  98.         arc_abort("Unable to create temporary file %s", buf);
  99.  
  100.     if (note)
  101.         printf("Invoking file: %s\n", hdr->name);
  102.  
  103.     for (n = 0; n < num; n++) {    /* add command line arguments */
  104.         strcat(buf, " ");
  105.         strcat(buf, arg[n]);
  106.     }
  107.  
  108.     dir = gcdir("");    /* see where we are */
  109.     unpack(arc, tmp, hdr);    /* unpack the entry */
  110.     fclose(tmp);        /* release the file */
  111.     chmod(buf, "700");    /* make it executable */
  112.     system(buf);        /* try to invoke it */
  113.     chdir(dir);
  114.     free(dir);        /* return to whence we started */
  115.     if (unlink(buf) && warn) {
  116.         printf("Cannot unsave temporary file %s\n", buf);
  117.         nerrs++;
  118.     }
  119. }
  120.